package com.wooshow.base.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wooshow.base.all.ApplicationContextHelper;
import com.wooshow.base.bean.SQLs;
import com.wooshow.base.dao.SQLsDao;

public class SQLsDaoHib extends HibernateDaoSupport implements SQLsDao {

	private static Log log = LogFactory.getLog(SQLsDaoHib.class);
	
	public SQLs getById(int id) {
		return getHibernateTemplate().get(SQLs.class, id);
    }

	public List<SQLs> listSQLs() {
		return getHibernateTemplate().find(" from SQLs ");
    }

	public int save(SQLs sqls) {
		getHibernateTemplate().saveOrUpdate(sqls);
	    return 0;
    }

	public static void main(String[] args) {
		log.debug("this is only for unit test");
		ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
		SQLsDao sqlsDao = (SQLsDao) ctx.getBean("sqlsDao");
		
		SQLs sqls = new SQLs();
		sqls.setSqlStr("update system...");
		sqls.setComments("English comments...");
		//sqlsDao.save(sqls);
		
		//sqls = sqlsDao.getById(6);
		//sqls.setComments("updated from unit test");
		sqlsDao.save(sqls);
		
		log.info(sqlsDao.listSQLs());
	}
	
}
